package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.MyPlanDetailsActivity;
import com.kkd.kkdapp.http.entity.MyPlanDataItem;
import com.kkd.kkdapp.view.RoundProgressBar;

public class MyPlanHistoryAdapter extends BaseAdapter {

	private Context context;
	private List<MyPlanDataItem> mList;
	private DecimalFormat df = new DecimalFormat("#,###");

	public MyPlanHistoryAdapter(Context context, List<MyPlanDataItem> mList) {
		this.mList = mList;
		this.context = context;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		OtherViewHolder mOtherViewHolder;
		if (convertView == null
				|| !(convertView.getTag() instanceof OtherViewHolder)) {
			mOtherViewHolder = new OtherViewHolder();
			convertView = View.inflate(context, R.layout.myregular_other_item,
					null);
			convertView.setTag(mOtherViewHolder);
		} else {
			mOtherViewHolder = (OtherViewHolder) convertView.getTag();
		}
		mOtherViewHolder.refund_name = (TextView) convertView
				.findViewById(R.id.refund_name);
		mOtherViewHolder.interest_tv = (TextView) convertView
				.findViewById(R.id.interest_tv);
		mOtherViewHolder.deadline_company_tv = (TextView) convertView
				.findViewById(R.id.deadline_company_tv);
		mOtherViewHolder.title = (RelativeLayout) convertView
				.findViewById(R.id.title);
		mOtherViewHolder.centre = (LinearLayout) convertView
				.findViewById(R.id.centre);
		mOtherViewHolder.bid = (TextView) convertView.findViewById(R.id.bid);
		mOtherViewHolder.term = (TextView) convertView.findViewById(R.id.term);
		mOtherViewHolder.company_tv = (TextView) convertView
				.findViewById(R.id.company_tv);
		mOtherViewHolder.top = (RelativeLayout) convertView
				.findViewById(R.id.top);
		mOtherViewHolder.company_tv1 = (TextView) convertView
				.findViewById(R.id.company_tv1);
		mOtherViewHolder.progressBar1 = (RoundProgressBar) convertView
				.findViewById(R.id.progressBar1);
		mOtherViewHolder.progressBar1.setVisibility(View.GONE);
		// 标的名称
		mOtherViewHolder.refund_name.setText(mList.get(position).getTitle());
		// 投标金额
		mOtherViewHolder.interest_tv.setText(df.format(mList.get(position)
				.getFinanceAmount() / 100));
		// 投标期限
		mOtherViewHolder.term.setText("" + mList.get(position).getClosedTerm());
		if ("天".equals(mList.get(position).getFinancePeriodType())) {
			mOtherViewHolder.company_tv.setText(mList.get(position)
					.getFinancePeriodType());
		} else if ("月".equals(mList.get(position).getFinancePeriodType())) {
			mOtherViewHolder.company_tv.setText("个"
					+ mList.get(position).getFinancePeriodType());
		}
		if (mList.get(position).getFinancePeriodWeb().indexOf("+") == -1) {
			mOtherViewHolder.company_tv1.setText("");
		} else {
			String[] bidperiodName = mList.get(position).getFinancePeriodWeb()
					.split("\\+");
			mOtherViewHolder.company_tv1.setText("+" + bidperiodName[1]);
		}
		// 投标日期
		mOtherViewHolder.deadline_company_tv.setText("投标时间 "
				+ mList.get(position).getFinanceDateString());
		mOtherViewHolder.bid.setVisibility(View.VISIBLE);
		mOtherViewHolder.bid
				.setBackgroundResource(R.drawable.tender_img_returnd);

		// 投标详情
		mOtherViewHolder.top.setOnClickListener(new OnClickListener() {

			String poiId = mList.get(position).getOrderId();

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, MyPlanDetailsActivity.class);
				intent.putExtra("poiId", poiId);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	class OtherViewHolder {
		TextView refund_name, bid;
		TextView interest_tv;
		TextView deadline_company_tv;
		RelativeLayout title, top;
		LinearLayout centre;
		TextView term;
		TextView company_tv, company_tv1;
		ImageView full_logo;
		RoundProgressBar progressBar1;
	}
}
